package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.OrderVo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryString;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderStatusService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderStatus")
public class OrderStatusController {

    @Reference
    private OrderStatusService orderStatusService;


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryString queryString){
        return orderStatusService.findPage(queryString);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Setmeal> list = orderStatusService.findAll();//发送请求
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer[] setmealIds) {
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            map.put("orderStatus", Order.ORDERSTATUS_NO);
            return orderStatusService.add(map,setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderVo orderVo){
        try{
            orderStatusService.edit(orderVo);
            return new Result(true,"编辑预约成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑预约失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            OrderVo order = orderStatusService.findById(id);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,order);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            orderStatusService.delete(id);
            return new Result(true,"删除预约数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除预约数据失败");
        }
    }

    @RequestMapping("/addOrderStatus")
    public Result addOrderStatus(@RequestBody OrderVo orderVo){
        try{
            orderStatusService.addOrderStatus(orderVo);
            return new Result(true,"更改预约数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"更改预约数据失败");
        }
    }

}
